package com.example.myapplication

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.SimpleAdapter
import android.widget.Spinner
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ActivityIcon_Icon : AppCompatActivity() {

    // 0. 数据类
    data class CityData(val icon: Int, val text: String, val intro: String)

    // 1. 定义城市列表
    private val cityList = listOf<CityData>(
        CityData(R.drawable.bj, "北京", "地道"),
        CityData(R.drawable.sh, "上海", "乡下人"),
        CityData(R.drawable.gz, "广州", "靓仔"),
        CityData(R.drawable.sz, "深圳", "双马尾"),
        CityData(R.drawable.hz, "杭州", "白娘子"),
        CityData(R.drawable.nj, "南京", "哈哈哈"),
    )

    // 2. 定义变量接收下拉列表
    private var spCity: Spinner? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_icon_icon)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        spCity = findViewById(R.id.spinner_icon)
        spCity?.adapter = SimpleAdapter(
            this,
            cityList.map {
                mapOf("icon" to it.icon, "text" to it.text, "intro" to it.intro)
            },
            R.layout.item_icon,
            arrayOf("icon", "text", "intro"),
            intArrayOf(R.id.icon, R.id.text, R.id.intro)
        )
        spCity?.setSelection(0)
        // 5. 监听选择了哪一项
        spCity?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                Toast.makeText(
                    this@ActivityIcon_Icon,
                    "你选择了" + cityList[position].text + cityList[position].intro,
                    Toast.LENGTH_SHORT
                ).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                TODO("Not yet implemented")
            }
        }
    }
}